﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Odyssey.Linq;
using System;

namespace Odyssey.Web.Models
{
    public class RouteTableManager
    {
        protected static OdysseyDataContext db
        {
            get { return DataContextManager.GetContext(); }
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            MapTopClassRoute(routes);
            MapArticleRoute(routes);
            MapSubClassRoute(routes);
            MapCustompageRoute(routes);
        }

        private static void MapArticleRoute(RouteCollection routes)
        {
            routes.MapRoute(
                "News", // Route name
                "News/List/{Id}", // URL with parameters
                new { controller = "News", action = "List" }, // Parameter defaults
                new string[] { "Odyssey.Web.Controllers" }
            );

            routes.MapRoute(
                "Article_Default", // Route name
                "Article/{id}", // URL with parameters
                new { controller = "Article", action = "Detail", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Odyssey.Web.Controllers" }
            );

            routes.MapRoute(
                "Article_UrlCode", // Route name
                "{TopClass}/{SubClass}/{UrlCode}", // URL with parameters
                new { controller = "Article", action = "View" }, // Parameter defaults
                new string[] { "Odyssey.Web.Controllers" }
            );


            //routes.MapRoute(
            //            "wbh/wbhgk",
            //            "wbh/wbhgk",
            //            new { controller = "Article", action = "Detail", id = 1 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "whb/zzjg",
            //            "whb/zzjg",
            //            new { controller = "Article", action = "Detail", id = 2 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "wbh/dwhts",
            //            "wbh/dwhts",
            //            new { controller = "Article", action = "Detail", id = 3 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "wbh/zpfw",
            //            "wbh/zpfw",
            //            new { controller = "Article", action = "Detail", id = 4 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "wbh/wjhg",
            //            "wbh/wjhg",
            //            new { controller = "Article", action = "Detail", id = 5 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "bjxx/zhjs",
            //            "bjxx/zhjs",
            //            new { controller = "Article", action = "Detail", id = 6 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "bjxx/sjdd",
            //            "bjxx/sjdd",
            //            new { controller = "Article", action = "Detail", id = 7 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "bjxx/zyhdlb",
            //            "bjxx/zyhdlb",
            //            new { controller = "Article", action = "Detail", id = 8 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "bjxx/zhc",
            //            "bjxx/zhc",
            //            new { controller = "Article", action = "Detail", id = 9 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "bjxx/fhc",
            //            "bjxx/fhc",
            //            new { controller = "Article", action = "Detail", id = 10 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "hzhb/hzpp",
            //            "hzhb/hzpp",
            //            new { controller = "Article", action = "Detail", id = 11 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "hzhb/hzmt",
            //            "hzhb/hzmt",
            //            new { controller = "Article", action = "Detail", id = 12 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "xwzx/xwmtsq",
            //            "xwzx/xwmtsq",
            //            new { controller = "Article", action = "Detail", id = 13 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );
        }

        private static void MapSubClassRoute(RouteCollection routes)
        {
            routes.MapRoute(
                "SubClass_Default", // Route name
                "SubClass/{id}", // URL with parameters
                new { controller = "SubClass", action = "List", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Odyssey.Web.Controllers" }
            );

            var subClassList = CacheHelper<tb_subclass>.GetAll();
            foreach (var subClass in subClassList)
            {
                if (!string.IsNullOrEmpty(subClass.UrlCode))
                {
                    var topClassUrlCode = CacheHelper<tb_topclass>.GetById(subClass.TopClassId).UrlCode;
                    if(string.IsNullOrEmpty(topClassUrlCode))
                    {
                        continue;
                    } 
                    routes.MapRoute(
                        subClass.Name_Zh_cn + "_SubClass_" + subClass.Id.ToString(), // Route name
                        topClassUrlCode + "/" + subClass.UrlCode, // URL with parameters
                        new { controller = "SubClass", action = "List", id = subClass.Id }, // Parameter defaults
                        new string[] { "Odyssey.Web.Controllers" }
                    );
                }
            }

            //var subclass = subClassList.Where(it => it.ClassUrl != null && it.ClassUrl != "");

            //var models = subclass.ToArray();
            //foreach (tb_subclass model in models)
            //{
            //    routes.MapRoute(
            //        model.ClassUrl,
            //        model.ClassUrl.TrimStart(new char[] { '/' }),
            //        new { controller = "SubClass", action = "List", id = model.Id },
            //        new string[] { "Odyssey.Web.Controllers" }
            //    );
            //}
        }

        private static void MapTopClassRoute(RouteCollection routes)
        {
            routes.MapRoute(
                "TopClass_Default", // Route name
                "TopClass/{id}", // URL with parameters
                new { controller = "TopClass", action = "List", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "Odyssey.Web.Controllers" }
            );

            var topClassList = CacheHelper<tb_topclass>.GetAll();
            foreach (var topClass in topClassList)
            {
                routes.MapRoute(
                            topClass.Name_Zh_cn,
                            topClass.UrlCode,
                            new { controller = "TopClass", action = "List", id = topClass.Id },
                            new string[] { "Odyssey.Web.Controllers" }
                        );

            }
            //routes.MapRoute(
            //            "gfxx",
            //            "gfxx",
            //            new { controller = "TopClass", action = "List", id = 1 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "wbh",
            //            "wbh",
            //            new { controller = "TopClass", action = "List", id = 1 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //            "zks",
            //            "zks",
            //            new { controller = "TopClass", action = "List", id = 2 },
            //            new string[] { "Odyssey.Web.Controllers" }
            //        );

            //routes.MapRoute(
            //    "xwzx",
            //    "xwzx",
            //    new { controller = "TopClass", action = "List", id = 2 },
            //    new string[] { "Odyssey.Web.Controllers" }
            //);
        }

        private static void MapCustompageRoute(RouteCollection routes)
        {
            var custompage = db.GetTable<tb_custompage>()
                        .Where(it => it.Url != null && it.Url != "");

            var models = custompage.ToArray();
            foreach (tb_custompage model in models)
            {
                routes.MapRoute(
                    string.Format("Custompage_{0}_{1}", model.Description, model.Url),
                    model.Url.TrimStart(new char[] { '/' }),
                    new { controller = "Custompage", action = "Detail", id = model.Id },
                    new string[] { "Odyssey.Web.Controllers" }
                );
            }
        }
    }
}